package indi.walty.dl8888_dirver.ui;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;

import javax.swing.JTextArea;
import javax.swing.JTextField;

import indi.walty.dl8888_dirver.Config;
import indi.walty.dl8888_dirver.dirver.DL8888;
import indi.walty.dl8888_dirver.dirver.PrnBuilder;

public class PrintListener implements MouseListener {
	private JTextField textWidth;
	private JTextField textHeight;
	private String path;
	public PrintListener(JTextField textWidth,JTextField textHeight,String path) {
		this.textWidth = textWidth;
		this.textHeight = textHeight;
		this.setPath(path);
	}
	
	private byte[] initByte() {
		String width = textWidth.getText();
		String height = textHeight.getText();
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		boolean b = false;
		try {
			b = PrnBuilder.buildPRN(width,height,path,baos);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return b ? baos.toByteArray() : null;
	}
	
	public static void saveFile(byte[] b) {
		File file = new File(Config.PRN_PATH);
		try {
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(b);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		byte[] b = initByte();
		saveFile(b);
		DL8888.sendMsg(Config.PRN_PATH);
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}


	public String getPath() {
		return path;
	}


	public void setPath(String path) {
		this.path = path;
	}

}
